package com.plian.system.controller.fr.bulletin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.plian.Tools.DateUtil;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.fr.bulletin.BulletinValue;
import com.plian.system.entity.fr.bulletin.NewBulletin;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.fr.bulletin.IBulletinValueService;
import com.plian.system.service.fr.bulletin.INewBulletinService;
import com.plian.system.vo.fr.bulletin.BulletinSummaryDetailVO;
import com.plian.system.vo.fr.bulletin.BulletinValueVO;
import com.plian.system.vo.fr.bulletin.NewBulletinVO;
import com.plian.system.wrapper.fr.bulletin.NewBulletinWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;

/**
 * @description:
 * @author: gangan
 * @create: 2021-02-03 11:41
 */

@RestController
@RequestMapping(value = "api/newbulletin")
@AllArgsConstructor
@Api(value = "新版快报", tags = "新版快报")
public class NewBulletinController {

    private INewBulletinService newBulletinService;

    private IBulletinValueService bulletinValueService;

    private NewBulletinWrapper newBulletinWrapper;

    @GetMapping("/init")
    public void init(){
        newBulletinService.oldDataToNew();
    }

    @PostMapping("/originReport")
    @ApiOperation(value = "初始化", notes = "传入reportTime yyyy-MM")
    @Resubmit(delaySeconds = 0)
    public R originReport(@RequestBody NewBulletin newBulletin){
        return newBulletinService.originReport(DateUtil.format(newBulletin.getReportTime(), DateUtil.PATTERN_MONTH));
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除", notes = "传入id")
    public R delete(@PathVariable String id){
        return newBulletinService.delete(id);
    }

    /**
     * 分页
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入Query")
    public ListResult<NewBulletinVO> list(@RequestBody Query query) {
        return newBulletinService.treeList(query);
    }

    @PostMapping(value = "/treeList")
    @ApiOperation(value = "子节点", notes = "传入reportTime(yyyy-MM), 单位id")
    public ListResult<NewBulletinVO> treeList(@RequestBody NewBulletin newBulletin) {
        return newBulletinService.childTree(newBulletin);
    }

    @PostMapping("/submit")
    @ApiOperation(value = "修改", notes = "传入NewBulletin")
    @Resubmit(delaySeconds = 0)
    public R submit(@RequestBody NewBulletin newBulletin){
        return newBulletinService.submit(newBulletin);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "明细", notes = "传入id")
    public R<NewBulletinVO> detail(@RequestBody NewBulletin newBulletin){
        NewBulletinVO detail = newBulletinWrapper.entityToVO(newBulletinService.getOne(Condition.getQueryWrapper(newBulletin)));
        detail.setBulletinValues(bulletinValueService.list(new QueryWrapper<BulletinValue>()
                .eq("bulletin_id", newBulletin.getId()).orderByAsc("sort_code")));
        R objResult = new R(CommonCode.SUCCESS);
        objResult.setPageData(detail);
        return objResult;
    }

    @PostMapping("/childList")
    @ApiOperation(value = "可合并表", notes = "传入reportTime yyyy-MM, orgId")
    public ListResult<NewBulletinVO> childList(@RequestBody NewBulletin newBulletin){
        return newBulletinService.childList(newBulletin);
    }

    @PostMapping(value = "/childSummary")
    @ApiOperation(value = "获取合并数据", notes = "传入勾选子表id, childIds")
    public ListResult<BulletinValue> childSummary(@RequestBody NewBulletin newBulletin){
        return newBulletinService.childSummary(newBulletin);
    }

    @PostMapping(value = "/getSummaryDetail")
    @ApiOperation(value = "获取合并明细", notes = "传入amountName, typeCode, bulletinId")
    public ListResult<BulletinSummaryDetailVO> getSummaryDetail(@RequestBody BulletinValueVO bulletinValueVO){
        return newBulletinService.getSummaryDetail(bulletinValueVO);
    }

    @PostMapping(value = "/uploadFile",produces="application/json;charset=UTF-8")
    @ApiOperation(value = "导入", notes = "传入导入文件, reportTime")
    public R<NewBulletin> uploadFile(@RequestParam("file") MultipartFile multipartFile, @RequestParam String reportTime){
        return newBulletinService.uploadFile(multipartFile, reportTime);
    }

    @PostMapping(value = "/exportExcel", produces = "application/json;charset=UTF-8")
    @ApiOperation(value = "导出", notes = "传入newBulletinVO")
    public void exportExcel(@RequestBody NewBulletinVO newBulletinVO, HttpServletResponse response, OutputStream os) throws Exception {
        newBulletinService.exportExcel(newBulletinVO, response, os);
    }

    @PostMapping(value = "/reportTree")
    @ApiOperation(value = "报表树", notes = "传入reportTime(yyyy-MM)")
    public ListResult<NewBulletinVO> reportTree(@RequestBody NewBulletin newBulletin) {
        return newBulletinService.reportTree(newBulletin);
    }
}
